﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView
{
    public partial class BianZhiControl : UserControl
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        EmpbasicService.BianZhiModel RoleModel = new EmpbasicService.BianZhiModel();
        private int OrgID = 0;
        public BianZhiControl()
        {
            InitializeComponent();
            Client.GetOrgForHRModelCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            Client.GetOrgForHRModelAsync(0);
        }

        void Client_GetOrgForHRModelCompleted(object sender, EmpbasicService.GetOrgForHRModelCompletedEventArgs e)
        {
            foreach (EmpbasicService.OrgForHRModel i in e.Result)
            {
                forumList.Add(i);
            }
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "全部单位", Name = "组织机构" });
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "党群系统" });
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "政法系统" });

            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "行政系统" });
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "参公系统" });
            //forumList.Add(new EmpbasicService.OrgForHRModel() { ID = 1, Type = "组织机构", Name = "地区系统" });
            AddTreeNode("全部单位", null);
        }
        ObservableCollection<EmpbasicService.OrgForHRModel> forumList = new ObservableCollection<EmpbasicService.OrgForHRModel>();


        private void AddTreeNode(string type, TreeViewItem treeViewItem)
        {
            List<EmpbasicService.OrgForHRModel> result = (from forumInfo in forumList
                                                          where forumInfo.Type == type
                                                          select forumInfo).ToList<EmpbasicService.OrgForHRModel>();

            if (result.Count > 0)
            {
                foreach (EmpbasicService.OrgForHRModel foruminfo in result)
                {
                    TreeViewItem objTreeNode = new TreeViewItem();

                    //StackPanel sp = new StackPanel();
                    CheckBox cb = new CheckBox();
                    cb.Content = foruminfo.Name;
                    cb.Tag = objTreeNode;
                    cb.DataContext = foruminfo;
                    //sp.Children.Add(cb);
                    cb.Click += new RoutedEventHandler(cb_Checked);
                    objTreeNode.Header = cb;

                    //objTreeNode.Header = foruminfo.Name;
                    //objTreeNode.DataContext = foruminfo;
                    //objTreeNode.Tag = foruminfo.ID;
                    if ((objTreeNode.Header as CheckBox).Content.ToString() == "组织机构") objTreeNode.IsExpanded = true;

                    //此样式将会添加的所有叶子结点上
                    //objTreeNode.ItemContainerStyle = this.Resources["RedItemStyle"] as Style;

                    //添加根节点
                    if (treeViewItem == null)
                    {
                        TreeOfLife.Items.Add(objTreeNode);
                    }
                    else
                    {
                        treeViewItem.Items.Add(objTreeNode);
                    }
                    AddTreeNode(foruminfo.Name, objTreeNode);
                }
            }
        }
        void cb_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            EmpbasicService.OrgForHRModel rm = cb.DataContext as EmpbasicService.OrgForHRModel;
            if (cb.IsChecked == true)
            {
                if (orgids != null)
                {
                    OrgID = Convert.ToInt32(((EmpbasicService.OrgForHRModel)cb.DataContext).ID);

                    if (OrgID != 0 && OrgID != 79 && OrgID != 80 && OrgID != 81 && OrgID != 82 && OrgID != 83 && OrgID != 84)
                    {
                        LoadData();
                    }
                }
                else orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());

                //CheckParentItem(cb.Tag as TreeViewItem);
                CheckChildItem(cb.Tag as TreeViewItem);
            }
            else
            {
                if (orgids != null)
                {
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                }
                UnCheckChildItem(cb.Tag as TreeViewItem);
            }
        }
        void CheckParentItem(TreeViewItem item)
        {
            TreeViewItem tvi = item.GetParentTreeViewItem();
            if (tvi != null)
            {
                CheckBox cb = tvi.Header as CheckBox;
                cb.IsChecked = true;
                CheckParentItem(tvi);
            }
        }
        public List<string> orgids = new List<string>();
        void CheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = true;
                    if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    CheckChildItem(tvi);
                }
            }
        }
        void UnCheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = false;
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    UnCheckChildItem(tvi);
                }
            }
        }
        private void TreeOfLife_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem item = e.NewValue as TreeViewItem;
            if (item != null)
            {
                if (item.Tag != null && Convert.ToInt32(item.Tag) != 0)
                {
                    OrgID = Convert.ToInt32(item.Tag);
                    LoadData();
                }
                
            }
        }

        void LoadData()
        {
            Client.GetBianZhiModelCompleted += new EventHandler<EmpbasicService.GetBianZhiModelCompletedEventArgs>(Client_GetBianZhiModelCompleted);
            Client.GetBianZhiModelAsync(OrgID, 0);
        }
        void Client_GetBianZhiModelCompleted(object sender, EmpbasicService.GetBianZhiModelCompletedEventArgs e)
        {
            DGRole.ItemsSource = null;
            DGRole.ItemsSource = e.Result;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            BianZhiWindow RMWindow = new BianZhiWindow(OrgID,0, true);
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            LoadData();
        }

        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as EmpbasicService.BianZhiModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                BianZhiWindow RMWindow = new BianZhiWindow(OrgID, Convert.ToInt32(seljw.ID), false);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的编制！"); 
        }
    }
}
